package com.mdlib.helper.ui.fragments.set;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.core.util.LogUtil;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.entity.ServiceEntity;
import com.mdlib.helper.model.entity.WebViewEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.ui.adapter.ServiceAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/11/19 09:38
 * 邮箱：hqy_xz@126.com
 */

public class ServiceFragment extends BaseTitleFragment {


    @Bind(R.id.rv_sercive_list)
    RecyclerView mRvSerciveList;


    private ServiceAdapter mServiceAdapter;

    private List<ServiceEntity> serviceList = new ArrayList<>();


    public static ServiceFragment newInstance() {
        return new ServiceFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_service;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.set_service_protocol, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        init();
    }

    private void init() {

        mServiceAdapter = new ServiceAdapter(serviceList);
        mRvSerciveList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvSerciveList.setAdapter(mServiceAdapter);
        mRvSerciveList.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemClick(adapter, view, position);
                UIHelper.showWeb(getActivity(), new WebViewEntity(serviceList.get(position).getTitle(),
                        serviceList.get(position).geturl()));
            }
        });
        getService();
    }

    private void getService() {
        addSubscribe(DataManager.getInstance().getUserApi()
                .getService()
                .compose(RxHelper.<ArrayList<ServiceEntity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<ServiceEntity>>() {
                    @Override
                    protected void onSucc(ArrayList<ServiceEntity> serviceEntities) {
                        serviceList = serviceEntities;
                        mServiceAdapter.setNewData(serviceList);
                        mServiceAdapter.notifyDataSetChanged();
                    }

                    @Override
                    protected void onFail(String msg) {
                        LogUtil.e("getService " +msg);
                    }
                }));
    }

}
